//
//  WSEmoticonAttachment.swift
//  emoticonView
//
//  Created by 武莎莎 on 15/9/16.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class WSEmoticonAttachment: NSTextAttachment {
    
    var chs:String
    
    init(chs:String) {
    
        self.chs = chs
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 返回表情的属性文本
    class func emoticonAttributeText(emoticon:WSEmoticons, font:UIFont) -> NSAttributedString {

        // 表情图片:textView的属性文本 图文混排
        let textattach = WSEmoticonAttachment(chs: emoticon.chs!)
        textattach.image = UIImage(named: emoticon.imagePath)
        
        // 设置表情图片的高度和文本一样高
        let height = font.lineHeight
        // bounds 就是contentOffSet
        textattach.bounds = CGRectMake(0, -4, height, height)
        
        // 设置图片的属性：属相文本字体的大小
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: textattach))
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        return imageText
    }
}